package com.cloudatum.redis;

import com.cloudatum.util.SpringUtil;
import org.apache.commons.lang3.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public final class RedisUtil {

    static JedisPool jedisPool = SpringUtil.getBean("jedisPool",JedisPool.class);

    /**
     * 获取Jedis实例
     * @return Jedis
     */
    public synchronized static Jedis getJedis() {
        return jedisPool.getResource();
    }

    /**
     * 释放jedis资源
     * @param jedis 要释放的jedis
     */
    public static void returnResource(final Jedis jedis) {
        if (jedis != null) {
            jedisPool.returnResource(jedis);
        }
    }

    public static void set(String key,String value){
        Jedis jedis = getJedis();
        jedis.set(key,value);
        returnResource(jedis);
    }

    public static String get(String key){
        Jedis jedis = getJedis();
        String str = jedis.get(key);
        returnResource(jedis);
        return str;
    }

}